package web.weibo.controller;

import web.weibo.mapper.MapperUser;
import web.weibo.model.User;
import web.weibo.service.ServiceUser;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class ControllerHelper {

    public static User currentUser(MapperUser mapperUser, HttpServletRequest request) {
        ServiceUser serviceUser = new ServiceUser(mapperUser);
        String username;
        Cookie[] cookies = request.getCookies();
        boolean found = false;
        String session_id = "";
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getName().equals("session_id")) {
                    session_id = cookie.getValue();
                    found = true;
                }
            }
        }
        if (found) {
            username = serviceUser.usernameFromSessionId(session_id);
        } else {
            username = "游客";
        }
        User user = serviceUser.userFromUsername(username);
        return user;
    }

    /**
     * 判断用户名格式
     * @param console
     * @return
     */
    public static boolean formatNote(String console) {
        int length = console.length();
        if (length > 0 && length <= 8) {
            return true;
        }
        return false;
    }

    /**
     * 判断账号格式是否规范
     *
     * @param console
     * @return
     */
    public static boolean formatName(String console) {
        int length = console.length();
        if (length >= 3 && length <= 8) {
            return format(console, length);
        }
        return false;
    }

    /**
     * 判断密码格式是否规范
     *
     * @param console
     * @return
     */
    public static boolean formatPass(String console) {
        int length = console.length();
        if (length >= 3 && length <= 8) {
            return format(console, length);
        }
        return false;
    }

    public static boolean format(String console, int length) {
        boolean result = false;
        String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String txt2 = txt.toLowerCase();
        // 所有字母
        String txt3 = txt.concat(txt2);
        String txt4 = "0123456789";
        // 字母加数字
        // 所有合格字符
        String txt5 = txt3.concat("_" + txt4);
        for (int i = 0; i < length; i++) {
            String s = String.valueOf(console.charAt(i));
            if (txt5.contains(s)) {
                result = true;
            } else {
                return false;
            }
        }
        return result;
    }

    public static boolean isUser(MapperUser mapperUser, String username) {
        int i = mapperUser.isUser(username);
        if (i == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isPassword(MapperUser mapperUser, String username, String password) {
        User currentUser = mapperUser.currentUser(username);
        String salt = currentUser.salt;
        String saltedPassword = ServiceUser.SaltedPasswordHash(password, salt);
        System.out.println("salt: " + salt);
        System.out.println("saltedPassword: " + saltedPassword);
        int i = mapperUser.isPassword(saltedPassword);
        System.out.println("这个值： " + i);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }
}
